<template>
  <!-- 第二步 章节处理  -->
  <div>
    <!-- 添加章节按钮 -->
    <div>
      <el-button type="primary" @click="addChapter()">添加章节</el-button>
    </div>

    <!-- 章节列表 -->
    <ul class="chapterList">
      <li v-for="chapter in chapterList" :key="chapter.id">
        <p>
          {{ chapter.title }}
          <span class="acts">
            <el-button type="text" @click="saveVideo(chapter.id)"
              >添加课时</el-button
            >
            <el-button type="text" @click="updateChapter(chapter.id)"
              >编辑</el-button
            >
            <el-button type="text" @click="deleteChapter(chapter.id)"
              >删除</el-button
            >
          </span>
        </p>

        <!-- 视频 -->
        <ul class="chapterList videoList">
          <li v-for="video in chapter.children" :key="video.id">
            <p>
              {{ video.title }}
              <el-tag v-if="!video.videoSourceId" size="mini" type="danger">
                {{ "尚未上传视频" }}
              </el-tag>
              <span class="acts">
                <el-tag v-if="video.free" size="mini" type="success">{{
                  "免费观看"
                }}</el-tag>
                <el-button type="text" @click="updateVideo(video.id)">编辑</el-button>
                <el-button type="text" @click="deleteVideo(video.id)"
                  >删除</el-button
                >
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>

    <!-- 章节表单对话框 TODO
        vue对象如果需要自定义查找组件,可以通过ref定义值,通过vue对象 this.$refs.key
     -->
    <chapter-form ref="chapterForm" />
    <!-- 课时表单对话框 TODO -->
    <video-form ref="videoForm" />
    <div style="text-align: center">
      <el-button type="primary" @click="previous">上一步</el-button>
      <el-button type="primary" @click="next">下一步</el-button>
    </div>
  </div>
</template>
<script>
// 引入组件
import ChapterForm from "@/views/course/components/Chapter/Form";
import ChapterApi from "@/api/chapter.js";

import VideoForm from "@/views/course/components/Video/Form";
import VideoApi from "@/api/video.js";
export default {
  data() {
    return {
      chapterList: [],
    };
  },
  created() {
    this.getChapterNestedList();
  },
  components: {
    ChapterForm,
    VideoForm,
  },
  methods: {
    //=====章节处理
    //添加章节
    addChapter() {
      this.$refs.chapterForm.title = "添加章节";
      //父子组件的通信方式
      this.$refs.chapterForm.open();
    },
    //查询章节课时嵌套集合
    getChapterNestedList() {
      ChapterApi.chapterNestedList(this.$parent.courseId).then((response) => {
        this.chapterList = response.data.items;
        //重新渲染,强制刷新
        this.$forceUpdate();
      });
    },
    //删除指定的章节
    deleteChapter(chapterId) {
      this.$confirm("此操作将永久删除该章节，是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //确认按钮
          return ChapterApi.removeChapter(chapterId);
        })
        .then((response) => {
          //重新刷新列表
          this.getChapterNestedList();
          this.$message.success(response.message);
        })
        .catch((response) => {
          //取消按钮
          if (response === "cancel") {
            this.$message.info("取消删除");
          }
        });
    },
    //更新章节
    updateChapter(chapterId) {
      this.$refs.chapterForm.title = "编辑章节";
      //查询要编辑的chapter数据,可以将id交给子组件来查询
      this.$refs.chapterForm.open(chapterId);
    },
    previous() {
      this.$parent.active = 0; //回到info基本信息页
    },
    next() {
      this.$parent.active = 2; //跳转到发布页
    },

    //=====课时处理
    //新增课时
    saveVideo(chapterId) {
      this.$refs.videoForm.title = '新增课时'
      //弹出video的对话框,并传递章节id 课程id
      this.$refs.videoForm.open(chapterId, this.$parent.courseId);
    },
    //删除课时
    deleteVideo(videoId) {
      this.$confirm("此操作将永久删除该课时，是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //确认按钮
          return VideoApi.deleteVideo(videoId);
        })
        .then((response) => {
          //重新刷新列表
          this.getChapterNestedList();
          this.$message.success(response.message);
        })
        .catch((response) => {
          //取消按钮
          if (response === "cancel") {
            this.$message.info("取消删除");
          }
        });
      //
    },
    //更新课时
    updateVideo(videoId){
      this.$refs.videoForm.title = '编辑课时'
      this.$refs.videoForm.open(null,null,videoId)
      
    }
  },
};
</script>
<style scoped>
.chapterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chapterList li {
  position: relative;
}
.chapterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chapterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dashed #ddd;
}
</style>